/* RISC-V最小操作系统主函数 */
/* 外部符号定义（来自链接脚本） */
extern char _bss_start[];
extern char _bss_end[];

/* 简单的内存地址定义 */
#define BSS_START 0x80010000
#define BSS_END   0x80011000

/* 串口函数声明 */
void uart_init(void);
void uart_putc(char c);
void uart_puts(char *s);

/* 实验二函数声明 */
int printf(const char *fmt, ...);
void clear_screen(void);
void console_init(void);

/* 简单的数字转字符串函数 */
void itoa(unsigned long num, char *str) {
    char *p = str;
    char tmp;
    if (num == 0) {
        *p++ = '0';
    } else {
        do {
            *p++ = '0' + (num % 10);
            num /= 10;
        } while (num);
    }
    *p-- = '\0';
    while (str < p) {
        tmp = *str;
        *str = *p;
        *p = tmp;
        str++;
        p--;
    }
}

/* 实验二测试函数 */
void test_experiment2(void) {
    uart_puts("\r\n");
    uart_puts("=== Experiment 2: Kernel printf and Clear Screen ===\r\n");
    
    // 测试基本格式化输出
    printf("Test integer: %d\r\n", 42);
    printf("Test negative: %d\r\n", -123);
    printf("Test hex: 0x%x\r\n", 0xABCDEF);
    printf("Test string: %s\r\n", "Hello, RISC-V!");
    printf("Test char: %c%c%c\r\n", 'A', 'B', 'C');
    printf("Test percent: 100%% complete\r\n");
    
    // 测试边界情况
    printf("Test INT_MAX: %d\r\n", 2147483647);
    printf("Test INT_MIN: %d\r\n", -2147483648);
    printf("Test NULL pointer: %s\r\n", (char*)0);
    printf("NULL string: %s\n", (char*)0);
    printf("Empty string: %s\n", "");
    uart_puts("\r\nExperiment 2 basic test completed!\r\n");
    
    // 等待然后清屏
    uart_puts("\r\nPreparing to clear screen...\r\n");
    
    // 简单延时
    for (volatile int i = 0; i < 1000000; i++);
    
    // 清屏测试
    clear_screen();
    printf("Screen cleared!\r\n");
    printf("This is new content after clearing...\r\n");
    
    // 综合测试
    printf("\r\n=== Comprehensive Test ===\r\n");
    printf("System boot successful!\r\n");
    printf("BSS segment: 0x%x - 0x%x\r\n", BSS_START, BSS_END);
    printf("Formatted output working correctly!\r\n");
}

/* 主函数 */
void main()
{
    /* 初始化串口和控制台 */
    uart_init();
    console_init();
    
    /* ========== 实验一测试 ========== */
    /* 输出启动信息 - 逐个字符输出避免字符串常量问题 */
    uart_putc('\r');
    uart_putc('\n');
    uart_putc('=');
    uart_putc('=');
    uart_putc('=');
    uart_putc(' ');
    uart_putc('E');
    uart_putc('x');
    uart_putc('p');
    uart_putc('e');
    uart_putc('r');
    uart_putc('i');
    uart_putc('m');
    uart_putc('e');
    uart_putc('n');
    uart_putc('t');
    uart_putc(' ');
    uart_putc('1');
    uart_putc(':');
    uart_putc(' ');
    uart_putc('B');
    uart_putc('o');
    uart_putc('o');
    uart_putc('t');
    uart_putc('l');
    uart_putc('o');
    uart_putc('a');
    uart_putc('d');
    uart_putc('e');
    uart_putc('r');
    uart_putc(' ');
    uart_putc('&');
    uart_putc(' ');
    uart_putc('O');
    uart_putc('S');
    uart_putc(' ');
    uart_putc('S');
    uart_putc('t');
    uart_putc('a');
    uart_putc('r');
    uart_putc('t');
    uart_putc('u');
    uart_putc('p');
    uart_putc(' ');
    uart_putc('=');
    uart_putc('=');
    uart_putc('=');
    uart_putc('\r');
    uart_putc('\n');
    
    uart_putc('H');
    uart_putc('e');
    uart_putc('l');
    uart_putc('l');
    uart_putc('o');
    uart_putc(' ');
    uart_putc('O');
    uart_putc('S');
    uart_putc('!');
    uart_putc('\r');
    uart_putc('\n');
    
    /* 显示BSS段信息 */
    char buffer[32];
    
    uart_putc('B');
    uart_putc('S');
    uart_putc('S');
    uart_putc(':');
    uart_putc(' ');
    
    itoa((unsigned long)BSS_START, buffer);
    for (char *p = buffer; *p; p++) {
        uart_putc(*p);
    }
    
    uart_putc(' ');
    uart_putc('-');
    uart_putc(' ');
    
    itoa((unsigned long)BSS_END, buffer);
    for (char *p = buffer; *p; p++) {
        uart_putc(*p);
    }
    
    uart_putc('\r');
    uart_putc('\n');
    
    uart_puts("Experiment 1 test completed!\r\n");
    
    /* ========== 实验二测试 ========== */
    test_experiment2();
    
    /* 系统挂起 */
    uart_puts("\r\nSystem running completed, entering idle loop...\r\n");
    while(1) {
        // 空闲循环
    }
}